import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController, ToastController} from 'ionic-angular';
//添加银行卡
import { AddBankPage } from '../add-bank/add-bank';
//设置支付密码
import { SetpaypwdPage } from '../setpaypwd/setpaypwd';

import { WithdrawcashPage } from '../withdrawcash/withdrawcash';

import { Https } from '../../app/http.service'
/**
 * Generated class for the BankcardPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-bankcard',
  templateUrl: 'bankcard.html',
})
export class BankcardPage {
	Url:string = new Https().Url + "act=goods&op=bank_info"; 
	deleteUrl: string = new Https().Url + "act=goods&op=deleteBankCard"; 
	pet: string = "kittens"; 
	data: string;
	state:number  = 0;
	bankList:string;
	fras:any;
	constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController, public toastCtrl: ToastController) {
	}

	ionViewWillEnter() {
		console.log('ionViewWillEnter BankcardPage');
		let thin = this;
		this.getList()
		
	}
	ionViewWillLeave(){
	}
	getList(){
		let thin = this;
		fetch(thin.Url, {
			method: 'post',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token"),
		}).then((responsce) => {
			responsce.text().then((json) => {
				let wdata = JSON.parse(json).datas
				console.log(wdata)
				console.log(wdata.bank_list + "====")
				
				// if (wdata.bank_list == null || wdata.bank_list == 'null') {
				if (wdata.bank_list != undefined) { //changed by kk
					thin.data = '0'
				} else {
					thin.data = '1'
					thin.bankList = wdata;
					console.log(thin.bankList);
				}
				console.log(thin.data);
				localStorage.setItem("payPwd", wdata.is_pay)
			})
		})
	}
	addbank(){
		if (localStorage.getItem("payPwd") == null || parseInt(localStorage.getItem("payPwd")) == 0){
			let confirm = this.alertCtrl.create({
				title: '温馨提示',
				message: '您尚未设置交易密码',
				buttons: [
					{
						text: '取消',
					},
					{
						text: '去设置',
						handler: () => {
							this.navCtrl.push(SetpaypwdPage);
						}
					}
				]
			});
			confirm.present();
		}else{
			this.navCtrl.push(AddBankPage); 	
		}
			

	}
	delete(id){
		let then = this;
		let confirm = this.alertCtrl.create({
			title: '温馨提示',
			message: '是否需要删除银行卡',
			buttons: [
				{
					text: '取消',
				},
				{
					text: '确定',
					handler: () => {
						console.log('delete');
						fetch(this.deleteUrl,{
							method: 'post',
							headers: {
								'Content-Type': 'application/x-www-form-urlencoded'
							},
							body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&id=" + id,
						}).then((responsce) => {
							responsce.text().then((data) => {
								if (data != ''){
									let json = JSON.parse(data).datas;
									if(json.code == '0'){
										then.presentToast('删除成功');
										then.ionViewWillEnter();
									}else{
										then.presentToast('网络错误');
									}
								}else{
									then.presentToast('网络错误');
								}
								
							})
						}).catch(() => {

						})
					}
				}
			]
		});
		confirm.present();
		
	}
	presentToast(content) {
		let toast = this.toastCtrl.create({
			message: content,
			duration: 3000
		});
		toast.present();
	}
	choose(id){
		console.log(id)
		this.navCtrl.pop()
		this.navCtrl.push(WithdrawcashPage,{
			bank_id:id
		})
		
	}
}
